// 轮播切换效果
+function ($) {
	'use strict';

	var Swiper = function (element, options) {
		this.$element			= $(element);
		this.options			 = $.extend({}, Swiper.DEFAULTS, options);
		this.$items = this.$element.find(this.options.selector.items);
		this.$pointers = this.$element.find(this.options.selector.pointers);
		this.sliding = false;
		this.waitTimer = null;
		this.swipTimer = null;
		this.bind();
		this.init();
	};

	Swiper.DEFAULTS = {
		auto: true,
		pause: false,
		interval: 5000,
		duration: 500,
		activeClass: "active",
		fadeInClass: "fadein",
		fadeOutClass: "fadeout",
		// doingClass: "swiping",
		selector: { items:".items", item:".item", pointers:".pointers", pointer:".pointer" }
	};

	Swiper.prototype.to = function (index) {
		if (this.sliding) { return; }
		this.sliding = true;
		var $items = this.$items.find(this.options.selector.item);
		var $currItem = $items.filter("." + this.options.activeClass);
		var currIndex = $items.index($currItem);
		if ($currItem.length == 0) { return; }
		var targetIndex;
		var $targetItem;
		if (index == "next") {
			$targetItem = $currItem.next();
			if ($targetItem.length == 0) {
				targetIndex = 0;
				$targetItem = $items.eq(targetIndex);
			} else {
				targetIndex = $items.index($targetItem);
			}
		} else if (index == "prev") {
			$targetItem = $currItem.prev();
			if ($targetItem.length == 0) {
				targetIndex = $items.length - 1;
				$targetItem = $items.eq(targetIndex);
			} else {
				targetIndex = $items.index($targetItem);
			}
		} else {
			targetIndex = index;
			$targetItem = $items.eq(targetIndex);
		}
		var self = this;
		if (targetIndex == currIndex) { return; }
		if ($targetItem.length == 0) { return; }
		self.options.doingClass && self.$element.addClass(self.options.doingClass);
		self.options.fadeOutClass && $currItem.addClass(self.options.fadeOutClass);
		self.options.fadeInClass && $targetItem.addClass(self.options.fadeInClass);
		window.setTimeout(function() {
			$currItem.removeClass(self.options.activeClass);
			$targetItem.addClass(self.options.activeClass);
		}, 1);
		if (self.$pointers.length) {
			var $pointers = self.$pointers.find(self.options.selector.pointer);
			$pointers.eq(currIndex).removeClass(self.options.activeClass);
			$pointers.eq(targetIndex).addClass(self.options.activeClass);
		}
		self.swipTimer = window.setTimeout(function() {
			self.sliding = false;
			self.options.doingClass && self.$element.removeClass(self.options.doingClass);
			self.options.fadeOutClass && $currItem.removeClass(self.options.fadeOutClass);
			self.options.fadeInClass && $targetItem.removeClass(self.options.fadeInClass);
			self.swipTimer = null;
		}, self.options.duration);
	};

	Swiper.prototype.pause = function (pause) {
		this.options.pause = !!pause;
		if (this.options.pause) {
			if (this.waitTimer != null) {
				window.clearTimeout(this.waitTimer);
				this.waitTimer = null;
			}
		} else {
			if (this.options.auto && this.waitTimer == null) {
				this.init();
			}
		}
	};
	Swiper.prototype.prev = function () {
		this.to("prev");
	};
	Swiper.prototype.next = function () {
		this.to("next");
	};

	Swiper.prototype.init = function () {
		if (!this.options.auto || this.options.pause || this.waitTimer != null) { return; }
		var self = this;
		this.waitTimer = window.setTimeout(function() {
			self.waitTimer = null;
			self.next();
			self.init();
		}, this.options.interval);
	};

	Swiper.prototype.bind = function () {
		var self = this;
		this.$element.hover(function() {
			self.pause(true);
		}, function() {
			self.pause(false);
		});
		this.$pointers.on("click", this.options.selector.pointer, function() {
			var index = self.$pointers.find(self.options.selector.pointer).index(this);
			self.to(index);
			self.pause(true);
		});
	};

	function Plugin(options) {
		return this.each(function () {
			var $this = $(this);
			var data = $this.data("bs.swiper");
			if (!data) {
				var o = $.extend({}, $this.zoptions(), $.isPlainObject(options) && options);
				$this.data("bs.swiper", (data = new Swiper(this, o)));
			}
			if ($.dt.isString(options)) { data[options](); }
		});
	};

	var old = $.fn.swiper;

	$.fn.swiper = Plugin;
	$.fn.swiper.Constructor = Swiper;

	$.fn.swiper.noConflict = function () {
		$.fn.swiper = old;
		return this;
	};

}(jQuery);
